package com.macro.mall.controller;

import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.PmsProductComment;
import com.macro.mall.service.OmsOrderReviewService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.Future;

@RestController
@Slf4j
public class OmsOrderReviewController {
    @Autowired
    private OmsOrderReviewService service;



    /**
     * 查看
     */
    @Operation(summary = "查看评论")
    @RequestMapping(value = "/lookComment/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<PmsProductComment>> lookComment(@PathVariable Integer id) {
        List<PmsProductComment> list = service.lookComment(id);
        return CommonResult.success(list);
    }
    /**
     * 添加评论
     */
    @Operation(summary = "添加评论")
    @RequestMapping(value = "/addComment", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<PmsProductComment> addComment(@RequestBody PmsProductComment comment) {
         service.addComment(comment);
        return CommonResult.success(null);
    }
    /**
     * 删除
     */
    @Operation(summary = "删除评论")
    @RequestMapping(value = "/delComment/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<PmsProductComment>> delComment(@PathVariable Integer id) {
        service.delComment(id);
        return CommonResult.success(null);
    }
    /**
     * 多线程获取返回值
     */
    @GetMapping("/gets")
    public void gets(){
        long l = System.currentTimeMillis();
        Future<Integer> title = service.title(1);
        Future<Integer> pmsViewCommentFuture = service.addView(1);

        Integer integer1 = 0;
        Integer integer2 = 0;

        while (true) {
            boolean done = title.isDone();
            boolean done2 = pmsViewCommentFuture.isDone();
            boolean b = done && done2;
            if (b) {
                try {
                    integer1 = title.get();
                    integer2 = pmsViewCommentFuture.get();
                    Thread.sleep(30);
                    break;
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
        Integer i = integer1 + integer2;
        log.info("执行结果为{}",i+"$");
        long l1 = System.currentTimeMillis() - l;
        System.out.println("耗时为:{}"+l1);
        log.info("耗时为:{}",System.currentTimeMillis()-l);

    }
}
